#!/usr/bin/env bash

help() {
    cat <<__EOF__
Usage: m powermode [OPTION] [ARGUMENT]

Description: Manage the power mode of your Mac

Options:
  --help                           Show this help message
  --status      <ac|battery|all>   Get the current power mode for the specified power source
  --set-battery <auto|low|high>    Set the power mode for Battery powermode
  --set-ac      <auto|low|high>    Set the power mode for AC powermode
  --set-all     <auto|low|high>    Set the power mode for both AC and Battery powermode
__EOF__
}

print_err() {
    echo "$@" 1>&2
}

get_ac_powermode_value(){
    local value=""
    value=$(pmset -g custom | grep -A5000 -m1 -e 'AC Power' | grep -i 'powermode' -m1 | awk '{print $2}')
    return $value
}

get_battery_powermode_value(){
    local value=""
    value=$(pmset -g custom | grep -A5000 -m1 -e 'Battery Power' | grep -i 'powermode' -m1 | awk '{print $2}')
    return $value
}

get_all_powermode_value(){
    local value=""
    value=$(pmset -g live | grep -i 'powermode' | awk '{print $2}')
    return $value
}

# convert to words
print_pretty_powermode(){
# get_powermode() {
    local powermode=""
    case $1 in
    0)
        powermode="auto"
        ;;
    1)
        powermode="low"
        ;;
    2)
        powermode="high"
        ;;
    *)
        echo "We can't read the power mode" && exit 1
        ;;
    esac
    echo "Power mode: $powermode"
}

set_powermode() {
    case $1 in
    ac)
        case $2 in
        auto)
            sudo pmset -c powermode 0
            ;;
        low)
            sudo pmset -c powermode 1
            ;;
        high)
            sudo pmset -c powermode 2
            ;;
        *)
            help
            ;;
        esac
        ;;
    battery)
        case $2 in
        auto)
            sudo pmset -b powermode 0
            ;;
        low)
            sudo pmset -b powermode 1
            ;;
        high)
            sudo pmset -b powermode 2
            ;;
        *)
            help
            ;;
        esac
        ;;
    all)
        case $2 in
        auto)
            sudo pmset -a powermode 0
            ;;
        low)
            sudo pmset -a powermode 1
            ;;
        high)
            sudo pmset -a powermode 2
            ;;
        *)
            help
            ;;
        esac
        ;;
    *)
        help && exit 1
        ;;
    esac
}

# Main script logic
option="$1"
shift

case $option in
    --help)
        help
        ;;
    --status)
        case $1 in
            ac)
                print_pretty_powermode $(get_ac_powermode_value)
                ;;
            battery)
                print_pretty_powermode $(get_battery_powermode_value)
                ;;
            all)
                print_pretty_powermode $(get_all_powermode_value)
                ;;
            *)
                help && exit 1
                ;;
        esac
        ;;
    --set-battery)
        if [[ -z $1 ]]; then
            print_err "Please specify a power mode (auto, low, high)"
            exit 1
        fi
        set_powermode battery "$1"
        ;;
    --set-ac)
        if [[ -z $2 ]]; then
            print_err "Please specify a power mode (auto, low, high)"
            exit 1
        fi
        set_powermode ac "$2"
        ;;
    --set-all)
        if [[ -z $2 ]]; then
            print_err "Please specify a power mode (auto, low, high)"
            exit 1
        fi
        set_powermode all "$2"
        ;;

    *)
        help && exit 1
        ;;
esac
